package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.OldPlayerReturnActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.task.args.PlayerReturnArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.oldplayerreturn.OldPlayerReturnModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.oldplayerreturn.OldPlayerReturnMgr;

import java.math.BigInteger;

/**
 * @author ：zfr
 * @date ：2021/12/6 15:51
 */
public class OldPlayerReturnOfflineDaysCondition extends BaseActivityCondition {

    public OldPlayerReturnOfflineDaysCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.OldPlayerReturnOfflineDays.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.OldPlayerReturnOfflineDays.getValue(), handle);
    }

    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        PlayerReturnArgs args = (PlayerReturnArgs) object;
        if (!isCompleted(player) && args.getOfflineDays() > getValue().intValue()) {
            setValue(BigInteger.valueOf(args.getOfflineDays()));
            setParam(String.valueOf(args.getReturnTime()));
            //回归了 初始化玩家回归数据
            if (isCompleted(player)) {
                OldPlayerReturnMgr.userReturn(player.getUserId(), activityInfo.getActivityId(), activityInfo.getType(), args.getReturnTime());
                if(OldPlayerReturnMgr.getNowActivityType() == OldPlayerReturnActivityType.NEW_ACTIVITY.getValue()){
                    OldPlayerReturnModule oldPlayerReturnModule = player.getModule(OldPlayerReturnModule.class);
                    oldPlayerReturnModule.recordPlayerVipLevelAndCalculatRecoverShopTimes(args.getOfflineDays(),true);
                    oldPlayerReturnModule.generateReturnUserLoginReward(true);
                    oldPlayerReturnModule.receiveTrialCard();
                }
                player.getModule(ActivityModule.class).syncActivity();
            }
        }
    }
}
